﻿using System;
using System.Windows.Input;
using Bulb.Core.Services.CollectionService;
using Bulb.Core.Services.DataStore;
using Cirrious.MvvmCross.Plugins.File;
using Cirrious.MvvmCross.Plugins.PictureChooser;
using Cirrious.MvvmCross.ViewModels;

namespace Bulb.Core.ViewModels
{
    public class AddBulbViewModel : BulbViewModel
    {
        public AddBulbViewModel(ICollectionService collectionService, IMvxPictureChooserTask pictureChooserTask, IMvxFileStore fileStore) : base(collectionService, pictureChooserTask, fileStore)
        {
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; RaisePropertyChanged(() => Name); }
        }

        private string _description;
        public string Description
        {
            get { return _description; }
            set { _description = value; RaisePropertyChanged(() => Description); }
        }

        private bool _state;
        public bool State
        {
            get { return _state; }
            set { _state = value; RaisePropertyChanged(() => State); }
        }

        private MvxCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                _saveCommand = _saveCommand ?? new MvxCommand(DoSave);
                return _saveCommand;
            }
        }

        private void DoSave()
        {
            if (!Validate(Name))
                return;
            
            CollectionService.Add(
                new BulbItem
                {
                    Name = Name, 
                    Description = 
                    Description, 
                    DateTime = DateTime.Now,
                    ImagePath = GenerateImagePath(ImagePath)
                }
            );

            ShowViewModel<DetailBulbViewModel>(new DetailBulbViewModel.Nav { Id = CollectionService.Latest.Id });
        }
    }
}
